package pathways.SpeciesSpace;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.sql.ResultSet;
import java.sql.SQLException;

import pathways.CCoCompound;
import pathways.Database.Implementation.CDBUpdate;
import pathways.Database.Implementation.CDatabase;
import pathways.util.Debug.CDebug;

public class CData
{
  private String reference = "";
  private long instantiatedRxnID = 0;
  private CField field = new CField();
  private String value = "";
  private String valueInDB = "";
  private long ID = 0;
  private int userID = 1; //???
  private int state = CCoCompound.NEW;  
                      // this is just an extra info variable
                      // that tells if in new state or deleted or normal state
                      // edited state cannot be detected from this var
                      // see save() to find out how states are determined 
      
  public CData()
  {
  }
  
  public String getReference()
  {
    return reference; 
  }
  
  public void setReference(String ref)
  {
    reference = ref;
  }
  
  public long getInstantiatedReactionID()
  {
    return instantiatedRxnID; 
  }
  
  public void setInstantiatedReactionID(int id)
  {
    instantiatedRxnID = id; 
  }
  
  public CField getField()
  {
    return field; 
  }
  
  public void setField(CField fld)
  {
    field = fld;
  }
  
  public String getValue()
  {
    return value; 
  }
  
  public void setValue(String fieldValue)
  {
    value = fieldValue;
  }
  
  public long getID()
  {
    return ID; 
  }
  
  public boolean read(int dataID, CDatabase db)
  {
    ResultSet rs = db.getSelect().getInstantiatedData(dataID);
    
    try {
      if (rs.next())
      {      
        ID = rs.getInt(1);
        field.read(rs.getInt(2),db);
        valueInDB = value = rs.getString(3);
        instantiatedRxnID = rs.getInt(4);
        userID = rs.getInt(5);
        reference = rs.getString(6);
        state = CCoCompound.NORMAL;
      }
      else
      {
        CDebug.println(this,"read() - No records found for DataID: " + dataID);
        return false; 
      }
    } catch (SQLException sqle) {
      CDebug.println(this,"read() - Exception: " + sqle.toString());
      return false; 
    }
          
    return true;
  }
  
  public boolean save(CDatabase db)
  {
    CDBUpdate upd = db.getUpdate();
    
    // is it new?
    if (valueInDB.equals(""))
    {
      if (!value.equals(""))
      {
        // new  
        ID = upd.newInstantiatedData(field.getID(),value,instantiatedRxnID,userID,reference);
        
        if (ID > 0)
        {
           valueInDB = value;
           state = CCoCompound.NORMAL;
        }
      }      
    }
    else
    {
      // in DB
      if (!valueInDB.equals(value))
      {
        if (value.equals(""))
        {
          if (ID > 0)
          {
             // delete this and reset its vars
             upd.deleteInstantiatedData(ID);
             valueInDB = "";
             ID = 0;
             instantiatedRxnID = 0;
             reference = "";
             userID = 0;
             state = CCoCompound.DELETE;
          }
        }
        else
        {
          // update
          upd.updateInstantiatedData(ID,field.getID(),value,reference);
          valueInDB = value;
        } 
      }
    }
    
    return true; 
  }
  
  public int getUserID()
  {
    return userID; 
  }
  
  public void setUserID(int id)
  {
    userID = id; 
  }
  
  public int getState()
  {
    return state; 
  }
  
  /*public void setState(int state)
  {
    this.state = state; 
  }*/
  

}